package zxing.activity.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import io.virtualapp.R;
import io.virtualapp.bean.Gds;
import io.virtualapp.widgets.MyOnItemClickListener;


/********************
 *  Created by ：  lyq
 *         Date：  2018\8\30 0030.
 * Introduction：
 *********************/

public class VipListAdapter extends RecyclerView.Adapter implements View.OnClickListener {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<Gds> mList = new ArrayList<>();
    private int gid;
    private MyOnItemClickListener onItemClickListener;
    private int selectPos = 0;

    private int getcurrentPosition() {
        return selectPos;
    }

    public VipListAdapter(Context mContext, List<Gds> list) {
        this.mContext = mContext;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
        selectPos = 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        VipMemberHolder holder = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_viplist, parent, false);
            holder = new VipMemberHolder(view);
            view.setTag(holder);
        } else {
            holder = (VipMemberHolder) view.getTag();
        }
        if (view != null) {
            view.setOnClickListener(this);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VipMemberHolder) {
            Gds gds = mList.get(position);
            VipMemberHolder vipMemberHolder = (VipMemberHolder) holder;
            vipMemberHolder.txt_monthvip1.setText("￥" + gds.getPrice() + "/");
            vipMemberHolder.txt_monthvip2.setText(gds.getName());
            vipMemberHolder.txt_youhui.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            vipMemberHolder.txt_youhui.getPaint().setAntiAlias(true);
            double i = Double.parseDouble(gds.getPrice());
            BigDecimal b1 = new BigDecimal(gds.getPrice());
            BigDecimal b2 = new BigDecimal("2");
            vipMemberHolder.txt_youhui.setText(b1.multiply(b2).doubleValue() + "元");
            switch (position) {
                case 0:
//                    vipMemberHolder.txt_price1.setText("会员专享：客服一对一福利大放送，永久会员限时半价！");
                    vipMemberHolder.txt_price2.setText("客服一对一专属服务");
                    vipMemberHolder.txt_price3.setText("会员不定期福利大放送");
                    vipMemberHolder.image_tuijian.setVisibility(View.GONE);
                    break;
                case 1:
                    vipMemberHolder.txt_price2.setText("限时优惠半价");
                    vipMemberHolder.txt_price3.setText("会员不定期福利大放送");
                    break;

            }


            if (position == selectPos) {
                vipMemberHolder.rv_item_parent.setBackground(mContext.getResources().getDrawable(R.drawable.vip_l));
                vipMemberHolder.txt_monthvip1.setTextColor(ContextCompat.getColor(mContext, R.color.t));
                vipMemberHolder.txt_monthvip2.setTextColor(ContextCompat.getColor(mContext, R.color.t));
                vipMemberHolder.view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.t));
                vipMemberHolder.txt_price1.setTextColor(ContextCompat.getColor(mContext, R.color.t));
                vipMemberHolder.txt_price2.setTextColor(ContextCompat.getColor(mContext, R.color.t));
                vipMemberHolder.txt_price3.setTextColor(ContextCompat.getColor(mContext, R.color.t));
                gid = gds.getGid();
            } else {
                vipMemberHolder.rv_item_parent.setBackground(mContext.getResources().getDrawable(R.drawable.vip_l_f));
                vipMemberHolder.txt_monthvip1.setTextColor(ContextCompat.getColor(mContext, R.color.f));
                vipMemberHolder.txt_monthvip2.setTextColor(ContextCompat.getColor(mContext, R.color.f));
                vipMemberHolder.view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.desktopColorD));
                vipMemberHolder.txt_price1.setTextColor(ContextCompat.getColor(mContext, R.color.f));
                vipMemberHolder.txt_price2.setTextColor(ContextCompat.getColor(mContext, R.color.f));
                vipMemberHolder.txt_price3.setTextColor(ContextCompat.getColor(mContext, R.color.f));
            }
            vipMemberHolder.itemView.setTag(position);
//            vipMemberHolder.rv_item_parent.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    currentPosition = position;
//                    notifyDataSetChanged();
//                }
//            });
        }
    }

    public int getGid() {
        return gid;
    }

    @Override
    public int getItemCount() {
        return mList.size() > 0 ? mList.size() : 0;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemcLick(v, (Integer) v.getTag());
        }
    }

    public void setSelectPos(int pos) {
        this.selectPos = pos;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(MyOnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
}
